package com.cq.smartelebicycle.view;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import com.cq.smartelebicycle.R;
import com.cq.smartelebicycle.fragment.WifiFragment;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WifiListAdapter extends BaseAdapter{
	
	private WifiFragment fragment;
	private Context context;
	private List<ScanResult> list;
    public final class ListItemView{ 

        public TextView wifiname;    
        public ImageView icpsw;    //是否有密码
        public ImageView wifisignal;              
 } 
	
	public WifiListAdapter(WifiFragment fragment,List<ScanResult> list){
		this.context = fragment.getActivity();
		this.fragment = fragment;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}
	
	public static int getSecurity(ScanResult result) {  
	    if (result.capabilities.contains("WEP")) {  
	        return 1;  
	    } else if (result.capabilities.contains("PSK")) {  
	        return 2;  
	    } else if (result.capabilities.contains("EAP")) {  
	        return 3;  
	    }  
	    return 0;  
	} 

	@Override
	public View getView(int index, View convertView , ViewGroup parent) {
		// TODO Auto-generated method stub
		ListItemView  listItemView = null;  
		final ScanResult wifi =list.get(index);
		if(convertView==null){
			listItemView = new ListItemView();
			convertView = LayoutInflater.from(context).inflate(R.layout.wifi_list_item, null);
			listItemView.wifiname = (TextView)convertView.findViewById(R.id.wifi_name);
			listItemView.icpsw =(ImageView)convertView.findViewById(R.id.wifi_psw_icon);
			listItemView.wifisignal=(ImageView)convertView.findViewById(R.id.wifi_signal_icon);
			convertView.setTag(listItemView);
			
		}else{
			listItemView = (ListItemView) convertView.getTag();
			
		}
		
		String name = wifi.SSID;
				//device.getName();
		Log.d("SIMMON","name="+name);
		if(name==null){
			name = wifi.BSSID;
		}
		listItemView.wifiname.setText(name);
		
		if(getSecurity(wifi) ==0){
			listItemView.icpsw.setVisibility(View.INVISIBLE);
		}else{
			listItemView.icpsw.setVisibility(View.VISIBLE);
			listItemView.icpsw.setImageResource(R.drawable.ic_wifi_psw);
		}
        //判断信号强度，显示对应的指示图标  
        if (Math.abs(wifi.level) > 100) {  
        	listItemView.wifisignal.setImageResource(R.drawable.ic_wifi_signal_0);  
        } else if (Math.abs(wifi.level) > 80) {  
        	listItemView.wifisignal.setImageResource(R.drawable.ic_wifi_signal_0);  
        } else if (Math.abs(wifi.level) > 70) {  
        	listItemView.wifisignal.setImageResource(R.drawable.ic_wifi_signal_0);  
        } else if (Math.abs(wifi.level) > 60) {  
        	listItemView.wifisignal.setImageResource(R.drawable.ic_wifi_signal_0);    
        } else if (Math.abs(wifi.level) > 50) {  
        	listItemView.wifisignal.setImageResource(R.drawable.ic_wifi_signal_0);  
        } else {  
        	listItemView.wifisignal.setImageResource(R.drawable.ic_wifi_signal_0); 
        }  
		
		return convertView;
	}
	
}
